package com.example.testanimation.truditionnal;

import com.scenic.rd_android.R;
import com.scenic.rd_android.R.id;
import com.scenic.rd_android.R.layout;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.Transformation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;

public class
		Animation_3DRotateAnimation_Activity extends Animation_LayoutAnimation_Activity {

	private ListView mList;

	private ViewGroup viewGroup;
	private ImageView mImgView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rotate_3d_animation);
		viewGroup = (ViewGroup) findViewById(R.id.id_flayout_mainContainer);
		mList = (ListView) findViewById(R.id.id_lstview_animation);
		mList.setScrollbarFadingEnabled(false);
		mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] {//
				"111111111111111111111111111111111111111111111111",//
						"111111111111111111111111111111111111111111111111",//
						"111111111111111111111111111111111111111111111111", //
						"111111111111111111111111111111111111111111111111",//
						"111111111111111111111111111111111111111111111111", //
						"111111111111111111111111111111111111111111111111", //
						"111111111111111111111111111111111111111111111111",//

				}));
		
		mImgView = (ImageView) findViewById(R.id.id_imgview_showDetail);
		
		mList.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				MyAnimation animation = new MyAnimation(0f,90f,(float)viewGroup.getWidth()/2, (float)viewGroup.getHeight()/2);
				animation.setDuration(1000);
				animation.setAnimationListener(new AnimationListener() {
					
					@Override
					public void onAnimationStart(Animation animation) {
						
					}
					
					@Override
					public void onAnimationRepeat(Animation animation) {
						
					}
					
					@Override
					public void onAnimationEnd(Animation animation) {
						mImgView.setVisibility(View.VISIBLE);
						mList.setVisibility(View.GONE);
						MyAnimation animationForImage = new MyAnimation(90f,180f,(float)viewGroup.getWidth()/2, (float)viewGroup.getHeight()/2);
						animation.setDuration(1000);
						viewGroup.startAnimation(animationForImage);
					}
				});
				viewGroup.startAnimation(animation);
			}
		});
		
	}

	
	class MyAnimation extends Animation {

		Camera camera;
		
		private float mCenterX;
		private float mCenterY;

		private float fromDegrees;

		private float mToDegrees;
		
		public MyAnimation(float fromDegress, float toDegress,float centerx,float centery) {
			this.fromDegrees = fromDegress;
			mToDegrees = toDegress;
			mCenterX = centerx;
			mCenterY = centery;
		}
		
//		@Override
		public void initializeInvalidateRegion(int left, int top, int right, int bottom) {
//			super.initializeInvalidateRegion(left, top, right, bottom);
			if(camera == null){
				camera = new Camera();
			}
		}
		@Override
		public void initialize(int width, int height, int parentWidth, int parentHeight) {
			super.initialize(width, height, parentWidth, parentHeight);
			if(camera == null){
				camera = new Camera();
			}
		}
		
		
		@Override
		protected void applyTransformation(float interpolatedTime, Transformation t) {
			
			Matrix matrix = t.getMatrix();
			float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
			
			System.out.println("applyTransformation " + interpolatedTime + "    " + degrees);
			camera.save();
			camera.translate(0, 0, 1000 * interpolatedTime);
			camera.rotateY(degrees);
			camera.getMatrix(matrix);
			camera.restore();
			matrix.preTranslate(-mCenterX, -mCenterY);
			matrix.postTranslate(mCenterX, mCenterY);
			
			
//			System.out.println("-----------------  " + camera.getLocationZ());
		}
		
	}
	
}
